package com.qfedu.buy.server.config;

import com.qfedu.buy.common.config.RabbitMQConstantConfig;
import com.qfedu.buy.common.config.SystemConfig;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


/**
 * @program: NeighborBuy
 * @description:
 * @author: Feri(邢朋辉)
 * @create: 2021-02-05 14:27
 */
@Configuration
public class RabbimqConfig {
    public static final String ex_order="llg-ex-skillorder";
    //死信交换器
    public String ex_dtx="llg-dtx-ex";
    //抢购订单的路由
    public String ex_skill_rk="llg-dtx-rk-skill";

    //创建队列 3个
    @Bean
    public Queue createSync(){
        return new Queue(RabbitMQConstantConfig.QUEUE_SKILL_ORDER);
    }
    @Bean
    public Queue createTTL(){
        //设置队列内部，消息的有效期
        Map<String,Object> map=new HashMap<>();
        //设置队列内部消息的有效期
        map.put("x-message-ttl", SystemConfig.SKILL_ORDER_TIME*1000);
        //设置死信交换器
        map.put("x-dead-letter-exchange",ex_dtx);
        //设置死信路由关键字
        map.put("x-dead-letter-routing-key",ex_skill_rk);
        return QueueBuilder.durable(RabbitMQConstantConfig.QUEUE_SKILL_ORDER_TTL).
                withArguments(map).build();
    }
    @Bean
    public Queue createDTX(){
        return new Queue(RabbitMQConstantConfig.QUEUE_SKILL_ORDER_DTX);
    }
    //创建交换器
    @Bean
    public FanoutExchange createExOrder(){
        return new FanoutExchange(ex_order);
    }
    @Bean
    public DirectExchange createExTimeout(){
        return new DirectExchange(ex_dtx);
    }
    //实现交换器和队列的绑定
    @Bean
    public Binding createBSync(FanoutExchange fe){
        return BindingBuilder.bind(createSync()).to(fe);
    }
    @Bean
    public Binding createBTtl(FanoutExchange fe){
        return BindingBuilder.bind(createTTL()).to(fe);
    }
    @Bean
    public Binding createBDtx(DirectExchange de){
        return BindingBuilder.bind(createDTX()).to(de).with(ex_skill_rk);
    }


}
